home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / src / pvmwin.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  7KB  |  355 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: pvmwin.c,v 1.1 1997/06/27 20:24:58 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. #include "pvmwin.h"
  34. #include <fcntl.h>
  35.  
  36. extern int debugmask;
  37.  
  38. char *username;
  39.  
  40.  
  41. void
  42. Wait(HANDLE id, DWORD *prv)
  43. {
  44.     if (!WaitForSingleObject(id, INFINITE)) {
  45.         *prv = 2;
  46.     } else {
  47.         if (!GetExitCodeThread(id, prv)) {
  48.             *prv = 4;
  49.         }
  50.     }
  51. }
  52.  
  53.  
  54. int win32_write_socket (SOCKET s,const char FAR *tosend, int size)
  55. {
  56.     int nSend=0;
  57.  
  58.     int size2send=size;
  59.  
  60.     while (size2send > 0) {
  61.         if ((nSend = send(s, tosend, size2send, 0)) == -1)
  62.             return nSend;
  63.  
  64.         tosend += nSend;
  65.         size2send -= nSend;
  66.     }
  67. /*
  68.     pvmlogprintf("loclinput, to send: %d yes, send: %d\n",
  69.             size,size-size2send);
  70. */
  71.     return size-size2send;    /* this should be $size in the end */
  72. }
  73.  
  74.  
  75. int win32_read_socket (SOCKET s,char FAR *torecv, int size)
  76. {
  77.     int nReceived=0;
  78.  
  79.     int size2recv=size;
  80.  
  81.     while (size2recv > 0) {
  82.  
  83.         if ((nReceived = recv(s, torecv, size2recv, 0)) == -1)
  84.             return nReceived;
  85.  
  86.         torecv += nReceived;
  87.         size2recv -= nReceived;
  88.     }
  89. /*
  90.     pvmlogprintf("loclinput, to read: %d yes, read: %d\n",
  91.             size,size-size2recv);
  92. */
  93.     return size-size2recv;
  94. }
  95.  
  96.  
  97. int gettimeofday(struct timeval *t, struct timezone *tzp)
  98. {
  99.     struct _timeb timebuffer;
  100.  
  101.     /* this calls the time and returns sec ** msec */
  102.     _ftime( &timebuffer );
  103.  
  104.     t->tv_sec=timebuffer.time;
  105.     t->tv_usec=timebuffer.millitm*1000;
  106.      return 1;
  107. }
  108.  
  109.  
  110. void nt_rpc_report( char *s)
  111. {
  112.     printf("Error: %s: %d\n",s,GetLastError());
  113. }
  114.  
  115.  
  116. void ErrorHandler(char *s)
  117. {
  118.     printf("Error: %s: %d\n",s,GetLastError());
  119. }
  120.  
  121.  
  122. int win32_close_file(HANDLE f)
  123. {
  124.     int success;
  125.     success = CloseHandle(f);
  126.     return success;
  127. }
  128.  
  129.  
  130. HANDLE win32_create_file(char *TMP_AREA, int mode)
  131. {
  132.     HANDLE hFile;
  133.     PSECURITY_DESCRIPTOR pSD;
  134.     PACL pACLNew;
  135.     DWORD cbACL = 1024;
  136.     PSID pSID;
  137.     DWORD cbSID = 1024;
  138.     LPSTR szUserName;
  139.     DWORD dwUserNameLength;
  140.     LPSTR lpszDomain;
  141.     DWORD cchDomainName = 80;
  142.     PSID_NAME_USE psnuType;
  143.  
  144.     /* Initialize a new security descriptor. */
  145.  
  146.  
  147.     pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
  148.         SECURITY_DESCRIPTOR_MIN_LENGTH);    /* defined in WINNT.H */
  149.     if (pSD == NULL) {
  150.         ErrorHandler("LocalAlloc");
  151.         return FALSE;
  152.     }
  153.  
  154.     if (!InitializeSecurityDescriptor(pSD,
  155.             SECURITY_DESCRIPTOR_REVISION)) {
  156.  
  157.         ErrorHandler("InitializeSecurityDescriptor");
  158.         goto Cleanup;
  159.     }
  160.  
  161.     /* Initialize a new ACL.                 */
  162.  
  163.     pACLNew = (PACL) LocalAlloc(LPTR, cbACL);
  164.     if (pACLNew == NULL) {
  165.         ErrorHandler("LocalAlloc");
  166.         goto Cleanup;
  167.     }
  168.  
  169.     if (!InitializeAcl(pACLNew, cbACL, ACL_REVISION2)) {
  170.         if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED) {
  171.             return (HANDLE) -2;
  172.         }
  173.         ErrorHandler("InitializeAcl");
  174.         goto Cleanup;
  175.     }
  176.  
  177.     /* Retrieve the SID for User        */
  178.     if (username)
  179.         szUserName=username;
  180.     else
  181.          szUserName=MyGetUserName();
  182.  
  183.     pSID = (PSID) LocalAlloc(LPTR, cbSID);
  184.     psnuType = (PSID_NAME_USE) LocalAlloc(LPTR, 1024);
  185.     lpszDomain = (LPSTR) LocalAlloc(LPTR, cchDomainName);
  186.     if (pSID == NULL || psnuType == NULL ||
  187.         lpszDomain == NULL) {
  188.         ErrorHandler("LocalAlloc");
  189.         goto Cleanup;
  190.     }
  191.  
  192.     if (!LookupAccountName((LPSTR) NULL, /* local name */
  193.             szUserName,
  194.             pSID,
  195.             &cbSID,
  196.             lpszDomain,
  197.             &cchDomainName,
  198.             psnuType)) {
  199.         ErrorHandler("LookupAccountName");
  200.         goto Cleanup;
  201.     }
  202.     if (!IsValidSid(pSID))
  203.         ErrorHandler("SID is not valid.\n");
  204.  
  205.  
  206.     /* Allow read but not write access to the file. */
  207.  
  208.     if (!AddAccessAllowedAce(pACLNew,
  209.             ACL_REVISION2,
  210.             GENERIC_ALL,
  211.             pSID)) {
  212.         ErrorHandler("AddAccessAllowedAce");
  213.         goto Cleanup;
  214.     }
  215.  
  216.     /* Add a new ACL to the security descriptor. */
  217.  
  218.     if (!SetSecurityDescriptorDacl(pSD,
  219.             TRUE,            /* fDaclPresent flag */
  220.             pACLNew,
  221.             FALSE)) {        /* not a default disc. ACL */
  222.         ErrorHandler("SetSecurityDescriptorDacl");
  223.         goto Cleanup;
  224.     }
  225.  
  226.     hFile = CreateFile(TMP_AREA,
  227.                     GENERIC_WRITE | GENERIC_READ,
  228.                     // FILE_SHARE_READ | FILE_SHARE_WRITE,
  229.                     0,
  230.                     NULL,
  231.                     mode,
  232.                     FILE_ATTRIBUTE_NORMAL,
  233.                     NULL);
  234.  
  235.     if (GetLastError() == ERROR_ALREADY_EXISTS)
  236.         if (mode == CREATE_NEW)
  237.             return (HANDLE) -1;
  238.  
  239.     if (hFile == INVALID_HANDLE_VALUE)
  240.     {
  241.         return (HANDLE) -1;
  242.     }
  243.  
  244.     /* Apply the new security descriptor to the file. */
  245.  
  246.     if (!SetFileSecurity(TMP_AREA,
  247.         DACL_SECURITY_INFORMATION,
  248.         pSD)) {
  249.         ErrorHandler("SetFileSecurity");
  250.         goto Cleanup;
  251.     }
  252.  
  253.     /*
  254.     printf("Successfully added access-allowed ACE to file's DACL.\n");
  255.     */
  256.  
  257.     return hFile;
  258.  
  259. Cleanup:
  260.         FreeSid(pSID);
  261.         if(pSD != NULL)
  262.             LocalFree((HLOCAL) pSD);
  263.         if(pACLNew != NULL)
  264.             LocalFree((HLOCAL) pACLNew);
  265.         if(psnuType != NULL)
  266.             LocalFree((HLOCAL) psnuType);
  267.         if(lpszDomain != NULL)
  268.             LocalFree((HLOCAL) lpszDomain);
  269.     return 0;
  270. }
  271.  
  272.  
  273. int win32_write_file(HANDLE f, char *s,int size)
  274. {
  275.     int sizewritten=0;
  276.  
  277.     if (!WriteFile(f,s,size,&sizewritten,NULL)) {
  278.         pvmlogprintf("Could not write to file :%d \n",GetLastError());
  279.         return 0;
  280.     }
  281.     return sizewritten;
  282.  
  283. }
  284.  
  285.  
  286. int win32_read_file(HANDLE f, char *s,int size)
  287. {
  288.     int sizeread=0;
  289.     if (!ReadFile(f,s,size,&sizeread,NULL)) {
  290.         pvmlogprintf("Could not read data from file :%d \n",
  291.                 GetLastError());
  292.         return -1;
  293.     }
  294.     return sizeread;
  295.  
  296. }
  297.  
  298.  
  299. HANDLE win32_open_file(char *TMP_AREA)
  300. {
  301.     HANDLE hF;
  302.  
  303.     hF= win32_create_file(TMP_AREA,OPEN_EXISTING);
  304.     if (hF == INVALID_HANDLE_VALUE)
  305.         printf("Could not open file %s: %d \n",TMP_AREA,GetLastError());
  306.     return hF;
  307. }
  308.  
  309.  
  310. int kill( int pid, int p_handle, int signal)
  311. {
  312.     HANDLE hProcess;
  313.  
  314.     // DebugBreak();
  315.     if (p_handle)
  316.     if (!TerminateProcess((HANDLE)p_handle,signal)) {
  317.         pvmlogprintf("Unable to terminate process: %d \n",p_handle);
  318.     } else return TRUE;
  319.  
  320.     hProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);
  321.  
  322.     if (hProcess) {
  323.         if (!TerminateProcess(hProcess,signal)) {
  324.             pvmlogprintf("Unable to terminate process: %d \n",hProcess);
  325.         } else {CloseHandle (hProcess);return TRUE;}
  326.     }
  327.     CloseHandle (hProcess);
  328.     return TRUE;
  329. }
  330.  
  331.  
  332. char *MyGetUserName()
  333. {
  334.     char *thisname=0;
  335.     char myuser[64];
  336.     DWORD szuser=64;
  337.     if (!GetUserName(myuser,&szuser)) {
  338.         pvmlogprintf("You are not logged on to this machine: %d\n",
  339.                 GetLastError());
  340.         pvmlogprintf("Continued by provided username\n");
  341.         return NULL;
  342.     }
  343.     thisname = malloc (16 *sizeof(char));
  344.     strncpy(thisname,myuser,16);
  345.     return thisname;
  346.  
  347. }
  348.  
  349.  
  350. int win32_delete_file(char *TMP_AREA)
  351. {
  352.     return (!DeleteFile(TMP_AREA));
  353. }
  354.  
  355.